package uk.ac.shef.speech_r25;

import java.text.DecimalFormat;
import java.util.Map;
import uk.ac.shef.wit.simmetrics.similaritymetrics.CosineSimilarity;
import yarp.Network;
import yarp.Bottle;
import yarp.Port;

/**
 * Hello world!
 *
 */
public class Listen extends Thread {

    Map<String, String> phrases;
    CosineSimilarity cosSim;
   String utterance;
    Robot robot;
   SpeechApp app;
   DecimalFormat df;
   String os;
   public Listen(SpeechApp app, String os) {
       this.os = os;
       this.app = app;
        phrases = Utils.readMap("resources/phrases.txt");
        cosSim = new CosineSimilarity();
        utterance = null;
        df = new DecimalFormat("#.##");
   }

    public void run() {
        System.out.println("Listen running.");
        YarpLoop loop = new YarpLoop(this, os);
        loop.start();
        while (true) {
            if (utterance!=null) {
                StringSim best = findMatch();
                if (best!=null) {
                    app.addPhrase(best.str);
                }
                utterance = null;
            }
            
            try {
                Thread.sleep(200);
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    void sendUtterance(String utterance) {
        this.utterance = utterance.replaceAll("\"", "").trim();
    }
    
    StringSim findMatch() {
        StringSim bestStr = findMostSimilar(utterance);
        String best = bestStr.str;
        float sim = bestStr.sim;
        
        if (sim>0.75) {
         System.out.println("High sim "+ df.format(sim) + " Heard "+ utterance + " sending sentence "+best);
            return bestStr;
        }
        else {
         System.out.println("Low sim "+ df.format(sim) + " Heard "+ utterance + " not sending sentence "+best);
            return null;
        }

    }

   

    StringSim findMostSimilar(String out) {
        String best = "NULL";
        float mostSim = 0;
        for (String phrase : phrases.keySet()) {
            float sim = calcSim(out, phrase);
            if (sim>mostSim) {
                mostSim = sim;
                best = phrase;
            }
        }
        StringSim strSim = new StringSim(best, mostSim);

        return strSim;
    }

    float calcSim(String out, String phrase) {
        return cosSim.getSimilarity(out, phrase);
    }

    void disableAllExcept(String[] string) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    void disableAll() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

class StringSim {
    String str;
    float sim;
    public StringSim(String str, float sim) {
        this.str = str;
        this.sim = sim;
    }
    public String toString() {
        return str+","+sim;
    }
}

class YarpLoop extends Thread {
    
    Listen listen;
    String os;
    YarpLoop(Listen listen, String os) {
        this.listen = listen;
        this.os = os;
    }
    public void run() {
        if (os.equals("linux")) {
            System.load("/home/samf/yarp/bindings/build/libjyarp.so");
        }
        else if (os.equals("windows")) {
            System.loadLibrary("jyarp");
        }
        System.out.println("yarp loop running");
      //  System.loadLibrary("/home/samf/yarp/bindings/build/libjyarp.so");
        Network.init();
        Port reader = new Port();
        //String portName = "/reader";
        reader.open("/hello/in");
        //Network.connect("/sender","/receiver");
        Bottle bot = new Bottle();
        while (true) {
            if (reader.read(bot)) {
                String utterance = bot.toString();     
                //System.out.println(word);
                listen.sendUtterance(utterance);
           }
        }
  }
}